package cn.tedu.circle.admin.content.controller;

import cn.tedu.circle.admin.content.pojo.vo.DynamicsListItemVO;
import cn.tedu.circle.admin.content.pojo.vo.DynamicsStandardVO;
import cn.tedu.circle.admin.content.service.IDynamicsService;
import cn.tedu.circle.common.pojo.vo.PageData;
import cn.tedu.circle.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/dynamics")
@Validated
@Api(tags = "获取动态列表")
public class DynamicsController {
    @Autowired
    IDynamicsService dynamicsService;

    @GetMapping("")
//    @PreAuthorize("hasAuthority('/content/article/simple')")
    @ApiOperation("查询文章列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int")
    })
    public JsonResult list(@Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【查询文章列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<DynamicsListItemVO> pageData = dynamicsService.list(pageNum);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/{id:[0-9]+}")
//    @PreAuthorize("hasAuthority('/content/article/simple')")
    @ApiOperation("根据ID查询文章")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的文章ID值！") Long id) {
        log.debug("开始处理【根据ID查询文章详情】的请求，参数：{}", id);
        DynamicsStandardVO queryResult = dynamicsService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }


    @PostMapping("/{id:[0-9]+}/delete")
//    @PreAuthorize("hasAuthority('/content/article/delete')")
    @ApiOperation("根据ID删除文章")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的文章ID值！") Long id) {
        log.debug("开始处理【根据ID删除文章】的请求，参数：{}", id);
        dynamicsService.deleteById(id);
        return JsonResult.ok();
    }

}
